{
    let EVENT_CLICK = Laya.Event.CLICK;

    class Match extends Sail.Scene {
        constructor (source, data) {
            super();

            this.bg = null;
            this.role = null;
            this.gameMode = null;
            this.roomList = null;
            this.mapIntro = null;
            this.members = null;
            this.chat = null;
            this.actions = null;
            this.btnBack = null;
            this.btnClose = null;

            this.curType = null; //match:匹配及房间列表 ready:准备及聊天界面
            this.mapId = null;
            this.actions = null;

            this.init(source, data);
        }

        destroy () {
            super.destroy();
            Sail.io.unregister(this.actions);
        }

        init (source, data) {
            console.log(data);
            this.curType = "match";

            let bg = new Laya.Image("res/match/bg.jpg");
                bg.scale(2, 2);
                bg.centerX = 0;
                bg.centerY = 0;

            let btnBack = new Laya.Image("res/match/btn_back.png");
                btnBack.on(EVENT_CLICK, this, this.goback);
                btnBack.left = 41;
                btnBack.top = 16;

            let btnClose = new Laya.Image("res/match/btn_close.png");
                btnClose.on(EVENT_CLICK, this, this.goback, [true]);
                btnClose.right = 42;
                btnClose.top = 15;

            this.bg = bg;
            this.btnBack = btnBack;
            this.btnClose = btnClose;
            this.role = Public.Role.I;
            this.gameMode = new Com.Match.GameMode(this.selectRole.bind(this));
            this.roomList = new Com.Match.RoomList(this.selectRole.bind(this));
            this.mapIntro = new Com.Match.MapIntro(data.battleIndex);
            this.members = new Com.Match.Members;
            this.chat = new Com.Match.Chat;

            this.addChildren(this.bg, this.btnBack, this.btnClose);
            
            this.actions = {
                "createTeam" (data, code, msg) {
                    if(code != 0){
                        Sail.director.popScene(new Alert.Tips(msg));
                        return;
                    }

                    this.mapIntro.update(data.map);
                    this.members.updateAll(data.user, data.map.id);
                    this.chat.updateRoomId(data.teamNo);

                    this.curType = "ready";
                    this.showMatchView();
                },
                "inputTeamNo" (data, code, msg) {
                    if(code != 0){
                        Sail.director.popScene(new Alert.Tips(msg));
                        return;
                    }
                    Sail.director.closeByName("enter_roomid");

                    this.mapIntro.update(data.map);
                    this.members.updateAll(data.user, data.map.id);
                    this.chat.updateRoomId(data.teamNo);

                    this.curType = "ready";
                    this.showMatchView();
                },
                "joinTeam" (data, code, msg) {
                    if(code != 0){
                        Sail.director.popScene(new Alert.Tips(msg));
                        return;
                    }

                    this.members.update(data);
                },
                "exit" (data, code, msg) {
                    if(code != 0){
                        Sail.director.popScene(new Alert.Tips(msg));
                        return;
                    }
                    if(data.type == 1){
                        this.curType = "match";
                        this.showMatchView();
                    }else{
                        if(data.uid == GM.user_id){
                            this.curType = "match";
                            this.showMatchView();
                            this.members.kickUser(1);
                            this.members.kickUser(2);
                        }else{
                            this.members.kickUser(data.pos);
                        }
                    }
                },
                "enter" (data, code, msg) {
                    if(code != 0){
                        Sail.director.popScene(new Alert.Tips(msg));
                        return;
                    }

                    Sail.director.closeByName("matching");

                    Laya.loader.load(ASSETS.LOADING, Laya.Handler.create(null, function () {
                        Sail.director.runScene(new Scene.Loading(data));
                    }));
                },
                "match" (data, code, msg) {
                    if(code != 0){
                        Sail.director.popScene(new Alert.Tips(msg));
                        return;
                    }

                    Sail.director.popScene(new Alert.Matching);
                },
                "exitMatch" (data, code, msg){
                    if(code != 0){
                        Sail.director.popScene(new Alert.Tips(msg));
                        return;
                    }
                    
                    for(let uid of data){
                        if(uid == GM.user_id){
                            Sail.director.closeByName("matching");
                        }
                    }
                }
            };
            Sail.io.register(this.actions, this);

            switch (source) {
                case "hall":
                    this.curType = "match";
                    this.mapId = data.mapId;
                    break;
                case "settle":
                    if(data.type == 1){
                        this.curType = "match";
                    }else if(data.type == 2){
                        this.curType = "ready";
                        this.members.updateAll(data.match, data.map.id);
                        this.chat.updateRoomId(data.tno);
                    }
                    this.mapIntro.update(data.map);
                    this.mapId = data.map.id;
                    break;
            }
            this.showMatchView();
            this.onResize(Laya.stage.width, Laya.stage.height);
            Sail.io.emit(GAME_CMDS.CHOOSEBATTLEMODE, { mapId: this.mapId });
        }

        showMatchView () {
            switch (this.curType) {
                case "match":
                    this.mapIntro.removeSelf();
                    this.members.removeSelf();
                    this.chat.removeSelf();
                    this.addChildren(this.gameMode, this.roomList);
                    break;
                case "ready":
                    this.gameMode.removeSelf();
                    this.roomList.removeSelf();
                    this.addChildren(this.mapIntro, this.members, this.chat);
                    break;
            }
        }

        selectRole (type) {
            switch (type) {
                case "pvp":
                    this.role.init(0, this.mapId, 2);
                    this.addChild(this.role);

                    break;
                case "pve":
                    this.role.init(0, this.mapId, 1);
                    this.addChild(this.role);

                    break;
                case "create":
                    Sail.io.emit("createTeam", {mapId : this.mapId});
                    break;
            }
        }

        goback (e, forceBackToHall) {
            if(forceBackToHall){
                Laya.loader.load(ASSETS.HALL, Laya.Handler.create(null, function () {
                    Sail.director.runScene(new Scene.Hall);
                }));
            }else{
                if(this.curType == "ready"){
                    Sail.io.emit("exit");
                }else{
                    Laya.loader.load(ASSETS.HALL, Laya.Handler.create(null, function () {
                        Sail.director.runScene(new Scene.Hall);
                    }));
                }
            }
        }

        onExit () {

        }

        onResize (width, height) {
            let rate = height / GAME_CONFIG.HEIGHT;
            rate = rate > 1 ? 1 : rate;

            this.gameMode.resize(width, height, rate);
            this.roomList.resize(width, height, rate);
            this.mapIntro.resize(width, height, rate);
            this.members.resize(width, height, rate);
            this.chat.resize(width, height, rate);
        }
    }

    Sail.class(Match, "Scene.Match");
}